<?php
	/**
	**     用户投票控制器
	**/
class Wxvote_con extends Lxbase {
	
	//投票列表
	public function show_vote(){
		
		$bs_id=$_SESSION['userinfo']['userid'];		
		$data_vote=$this->arrdata("*","wxvote","where bs_id=$bs_id");
		$this->smarty->assign("data_vote",$data_vote);
		$this->smarty->display("wx_show_vote.tpl");
	}
	//获取投票的详情信息
	public function detail_vote(){
		$id=$_GET['id'];
		$data_vote=$this->viewdata("*","wxvote","where id=".$id);
		$data_question=$this->arrdata("*","wxquestion","where vote_id=".$id);
		$question_index=1;
		foreach ($data_question as  $key=>$value) {
			$vote_numbers=$value['vote_numbers'];
			$options=unserialize($value['options']);
			$new_options = array();
			if(is_array($options)){
				foreach ($options as $k1=> $v1) {
					$numbers=$v1['numbers'];

					$new_options[$k1]['option_name'] = $v1['option_name'];
					$new_options[$k1]['img_url'] = $v1['img_url'];
					$new_options[$k1]['numbers'] = $v1['numbers'];
					if($numbers && $vote_numbers) {
						$new_options[$k1]['baifenbi']=round($numbers/$vote_numbers*100,2);
					}else{
						$new_options[$k1]['baifenbi'] = 0;
					}
				}

			}
			$data_question[$key]['options'] = $new_options;
				$data_question[$key]['question_index']=$question_index;
				$question_index++;	
				//$data_question[$key]['options']=$options;
		}
		$this->smarty->assign("data_vote",$data_vote);
		$this->smarty->assign("data_question",$data_question);		
		$this->smarty->display("wx_deatil_vote.tpl");
	}
	//新建投票
	public function add_vote(){		

		if($_POST){		

			$bs_id=$_SESSION['userinfo']['userid'];
			$name=$_POST['name'];
			$vote_pri=$_POST['vote_pri'];
			$stop_time=$_POST['stop_time'];
			$stop_time = str_replace("年", "-", $stop_time);			
			$stop_time = str_replace("月", "-", $stop_time);
			$stop_time = str_replace("日", "", $stop_time);
			$stop_time = strtotime($stop_time);		
			$params_vote=array(
				'name'=>addslashes($name),
				'vote_pri'=>$vote_pri,
				'bs_id'=>$bs_id,
				'create_time'=>time(),
				'stop_time'=>$stop_time,				
			);

			$vote_id=$this->setdata("wxvote",$params_vote);

			//处理问题
			$question_list=$_POST['question'];
			if(!$question_list){
				return '';
			}
			
			foreach ($question_list as $question) { 
				if($question['options']){
					$options=array();
					foreach ($question['options'] as $key => $option) {
						$options[$key]['option_name']=$option['option_name'];
						$options[$key]['img_url']=$option['img_url'];
						$options[$key]['numbers']=0;
					}
				}
				//组装问题数据
			$params_question=array(
				'vote_id'=>$vote_id,
				'bs_id'=>$bs_id,
				'title'=>addslashes($question['title']),
				'slec_option'=>$question['slec_option'],
				'options'=>serialize($options),
				'vote_numbers'=>0,
			);			
			$question_id=$this->setdata("wxquestion",$params_question);
			
			}
			echo json_encode(array('error'=>'1000','msg'=>'add success'));
			exit;
				}else{
					$this->smarty->display("wx_add_vote.tpl");	
				}	
			
	}	
	//图片上传
	public function up_img(){
		$imgname='img_url';//图片的名字
		if(empty($_FILES[$imgname]["size"])){
			die(json_encode(array('error'=>1001,"msg"=>"请上传正确的图片文件")));
		}
		//var_dump($_FILES[$imgname]);
		$srcInfo = getimagesize($_FILES[$imgname]["tmp_name"]);
		if(!in_array($srcInfo[2],array(1,2,3,6))){
			die(json_encode(array('error'=>1002,"msg"=>"上传格 式只支持jpg,gif,png,请选择正确的格式上传")));
		}
	
		$picdata = $this->upload($_FILES[$imgname],'wxvote');
		if(empty($picdata)){
			echo json_encode(array("error"=>1003,"msg"=>"上传图片失败，请重试"));
			exit;
		}
		$picurl=$picdata["pic"];
			$v_data=array('error'=>1000,"msg"=>"上传成功","pic"=>$picdata["pic"]);
		echo json_encode($v_data);
		exit;
	}
	//修改投票截止时间
	public function update_stop_time(){
	
		$vote_id=$_REQUEST['vote_id'];	
		$stop_time=$_REQUEST['stop_time'];
		if($vote_id && $stop_time){			
			$stop_time = str_replace("年", "-", $stop_time);			
			$stop_time = str_replace("月", "-", $stop_time);
			$stop_time = str_replace("日", "", $stop_time);
			$stop_time = strtotime($stop_time);			
			$where="where id=$vote_id";
			$set="stop_time=$stop_time";			
			$res=$this->updata("wxvote",$set,$where);
			echo json_encode(array('msg'=>'true'));
			exit;
		}
		exit;
		
	}
	//删除投票
	public function del_vote(){
		$bs_id=$this->userinfo['userid'];
		$vote_id=$_GET['id'];
		$this->deldata("wxvote","where id={$vote_id} and bs_id=$bs_id");
		$this->deldata("wxquestion","where vote_id={$vote_id} and bs_id=$bs_id");
		header("location:?index.php&g=wx&m=wxvote&a=show_vote");
		
	} 
}